// WndGps.cpp : implementation file
//

#include "stdafx.h"
#include "BBViewer.h"
#include "WndGps.h"

#include "StatusMgr.h"

enum 
{
	Gps_Title = 0,
	Gps_Status,
	Gps_Time,
	Gps_Lat,
	Gps_Long,
	Gps_Time_Start,
	Gps_Time_End,
	Gps_speed,
};

// CWndGps

IMPLEMENT_DYNAMIC(CWndGps, CWnd)

CWndGps* g_pWndGps = NULL;
#define TIMER_ID_CURRENT_TIME			0x1001


CWndGps::CWndGps()
{
	g_pWndGps		= this;
	m_tStartTime	= 0;
	m_tEndTime		= 0;
	m_tGpsTime		= time(0);
	m_fLatitude		= 0;
	m_fLongitude	= 0;
	m_fSpeed = 0;
	m_nUnit = 0;

	m_bPlayOn = FALSE;

	m_nGpsWndMode = GPS_SHOW_NORMAL;

	LOGFONT logFont;
	logFont = g_FontGps;
	logFont.lfWeight = FW_NORMAL;
	logFont.lfHeight = 14;

	m_fntText.CreateFontIndirect(&logFont);


}

CWndGps::~CWndGps()
{
	g_pWndGps		= NULL;

	m_penEdge.DeleteObject();
	m_brushBoard.DeleteObject();
	m_brushTextBk.DeleteObject();
	m_brushMark.DeleteObject();
	m_fntText.DeleteObject();
}


BEGIN_MESSAGE_MAP(CWndGps, CWnd)
	ON_MESSAGE(WM_CTL_MESSAGE, OnCtlMessage)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_TIMER()
	ON_WM_DESTROY()
END_MESSAGE_MAP()



// CWndGps message handlers



void CWndGps::OnPaint()
{
	CPaintDC dc(this); 

	LPST_BMP_INFO lpBmpInfo = theApp.BmpInfo.GetAt(biMapBG);
	if (lpBmpInfo->hbmpImage != NULL)
	{
		CRect rect;
		CBitmap bmp, *pOldBmp;
		CDC dcMemory;

		CBitmap *pBmpBg, *pOldBmpBg;
		CDC dcBg;
		LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpGpsView);
		CRect rtDiv = lpWndPos->rtDiv;
		CRect rtImg = lpBmpInfo->rtImageRect;

		this->GetClientRect(&rect);

		bmp.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());	//(1)-create
		dcMemory.CreateCompatibleDC(&dc);								//(2)-create

		pOldBmp = dcMemory.SelectObject(&bmp);							//(3)-select

		dcBg.CreateCompatibleDC(&dc);									//(4)-create
		pBmpBg = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
		pOldBmpBg = dcBg.SelectObject(pBmpBg);							//(5)-select

		dcMemory.BitBlt(0, 0, rtDiv.left, rtDiv.top, &dcBg, 0, 0, SRCCOPY);
		dcMemory.StretchBlt(rtDiv.left, 0, rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.top,
			&dcBg, rtDiv.left, 0, rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.top, SRCCOPY);
		dcMemory.BitBlt(rect.Width()-rtDiv.right, 0, rtDiv.right, rtDiv.top,
			&dcBg, rtImg.Width()-rtDiv.right, 0, SRCCOPY);
		dcMemory.StretchBlt(0, rtDiv.top, rtDiv.left, rect.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBg, 0, rtDiv.top, rtDiv.left, rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);
		dcMemory.StretchBlt(rtDiv.left, rtDiv.top,
			rect.Width()-(rtDiv.left+rtDiv.right), rect.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBg, rtDiv.left, rtDiv.top,
			rtImg.Width()-(rtDiv.left+rtDiv.right), rtImg.Height()-(rtDiv.top+rtDiv.bottom),
			SRCCOPY);
		dcMemory.StretchBlt(rect.Width()-rtDiv.right, rtDiv.top, rtDiv.right, rect.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBg, rtImg.Width()-rtDiv.right, rtDiv.top,
			rtDiv.right, rtImg.Height()-(rtDiv.top+rtDiv.bottom),
			SRCCOPY);
		dcMemory.BitBlt(0, rect.Height()-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
			&dcBg, 0, rtImg.Height()-rtDiv.bottom, SRCCOPY);
		dcMemory.StretchBlt(rtDiv.left, rect.Height()-rtDiv.bottom,
			rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			&dcBg, rtDiv.left, rtImg.Height()-rtDiv.bottom,
			rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			SRCCOPY);
		dcMemory.BitBlt(rect.Width()-rtDiv.right, rect.Height()-rtDiv.bottom, rtDiv.right, rtDiv.bottom,
			&dcBg, rtImg.Width()-rtDiv.right, rtImg.Height()-rtDiv.bottom, SRCCOPY);

		dc.BitBlt(0, 0, rect.Width(), rect.Height(), &dcMemory, 0, 0, SRCCOPY);

		dcBg.SelectObject(pOldBmpBg);		//(5) de-select
		dcBg.DeleteDC();					//(4) delete

		dcMemory.SelectObject(pOldBmp);		//(3) de-select
		bmp.DeleteObject();					//(1) delete		//2013.04.29
		dcMemory.DeleteDC();				//(2) delete
	}

	UpdateGPS2();
}

void CWndGps::SetPlayBlockTime(DWORD dwStart, DWORD dwEnd)
{
	m_tStartTime = dwStart;
	m_tEndTime	 = dwEnd;
}

void CWndGps::SetGpsInfo(DWORD dwTime, float fLatitude, float fLongitude, float fSpeed, int nUnit)
{
	m_tGpsTime		= (time_t)dwTime;
	m_fLatitude		= fLatitude;
	m_fLongitude	= fLongitude;

	m_fSpeed = fSpeed;
	m_nUnit = nUnit;

	DWORD dwTextType;
	if (m_nMode == 2)
	{
		dwTextType = (1<<CURRENT_TIME)|
					(1<<START_END_TIME)|
					(1<<EXT_END_TIME)|
					(1<<LONGITUDE)|
					(1<<LATITUDE)|
					(1<<DRIVING_SPEED);
	}
	else
	{
		switch (m_nGpsWndMode)
		{
		case GPS_HIDE:
		case GPS_SHOW_NORMAL:
			dwTextType = (1<<CURRENT_TIME);
			if (m_nMode == 3)
				dwTextType |= (1<<START_END_TIME);
			break;
		case GPS_SHOW_LARGE:
			if ((m_nMode == 0)||(m_nMode == 3))
				dwTextType = 0x0F;
			else if (m_nMode == 1)
				dwTextType = (1<<CURRENT_TIME)|
							(1<<START_END_TIME)|
							(1<<EXT_END_TIME)|
							(1<<LONGITUDE)|
							(1<<LATITUDE);
			break;
		}
	}
	TextOutGps(dwTextType);

}

void CWndGps::SetSpeedUnit(int nUnit)
{
	m_nUnit = nUnit;
	if (m_nMode == 2)
	{
		DWORD dwTextType = (1<<CURRENT_TIME)|
			(1<<START_END_TIME)|
			(1<<EXT_END_TIME)|
			(1<<LONGITUDE)|
			(1<<LATITUDE)|
			(1<<DRIVING_SPEED);

		TextOutGps(dwTextType);
	}
}

void CWndGps::SetGpsInfo(DWORD dwTime, float fLatitude, float fLongitude)
{
	m_tGpsTime		= (time_t)dwTime;
	m_fLatitude		= fLatitude;
	m_fLongitude	= fLongitude;

	DWORD dwTextType;
	if (m_nMode == 2)
	{
		dwTextType = (1<<CURRENT_TIME)|
					 (1<<START_END_TIME)|
					 (1<<EXT_END_TIME)|
					 (1<<LONGITUDE)|
					 (1<<LATITUDE)|
					 (1<<DRIVING_SPEED);
	}
	else
	{
		switch (m_nGpsWndMode)
		{
		case GPS_HIDE:
		case GPS_SHOW_NORMAL:
			dwTextType = (1<<CURRENT_TIME);
			break;
		case GPS_SHOW_LARGE:
			if ((m_nMode == 0) || (m_nMode == 3))
				dwTextType = 0x0F;
			else if (m_nMode == 1)
				dwTextType = (1<<CURRENT_TIME)|
							 (1<<START_END_TIME)|
							 (1<<EXT_END_TIME)|
							 (1<<LONGITUDE)|
							 (1<<LATITUDE);
			break;
		}
	}
	TextOutGps(dwTextType);
}

void CWndGps::TextOutGps(DWORD dwTextType)
{
	if (m_nMode == 0)
		TextOutGps_m0(dwTextType);
	else if (m_nMode == 3)
		TextOutGps_m0(dwTextType);
	else if ((m_nMode == 1)||(m_nMode == 2))
		TextOutGps_m1(dwTextType);

}

void CWndGps::UpdateGPS_Large(DWORD dwTextType)
{
	if (m_nMode == 0)
		UpdateGPS_Large_m0();
	else if (m_nMode == 3)
		UpdateGPS_Large_m0();
	else if ((m_nMode == 1)||(m_nMode == 2))
		UpdateGPS_Large_m1(dwTextType);
}


void CWndGps::UpdateGPS_Normal()
{
	if (m_nMode == 0)
		UpdateGPS_Normal_m0();
	else if (m_nMode == 3)
		UpdateGPS_Normal_m3();
	else if (m_nMode == 1)
		UpdateGPS_Normal_m1();
	else if (m_nMode == 2)
	{
		DWORD dwTextType = (1<<CURRENT_TIME)|
						   (1<<START_END_TIME)|
						   (1<<EXT_END_TIME)|
						   (1<<LONGITUDE)|
						   (1<<LATITUDE)|
						   (1<<DRIVING_SPEED);

		UpdateGPS_Large_m1(dwTextType);
	}
}

void CWndGps::SetPlayStatus(BOOL bPlayOn)
{
	m_bPlayOn = bPlayOn;
	if (m_bPlayOn == FALSE)
	{
		m_fLatitude = 0;
		m_fLongitude = 0;
		m_fSpeed = 0;
	}

	UpdateGPS2();
}

void CWndGps::UpdateGPS2()
{
	DWORD dwTextType = 0;

	if (m_nMode == 2)
	{
		dwTextType = (1<<CURRENT_TIME)|
					 (1<<START_END_TIME)|
					 (1<<EXT_END_TIME)|
					 (1<<LONGITUDE)|
					 (1<<LATITUDE)|
					 (1<<DRIVING_SPEED);

		UpdateGPS_Large(dwTextType);
	}
	else
	{
		switch (m_nGpsWndMode)
		{
		case GPS_HIDE:
		case GPS_SHOW_NORMAL:
			UpdateGPS_Normal();
			dwTextType = 1<<CURRENT_TIME;
			if (m_nMode == 3)
				dwTextType |= 1<<START_END_TIME;
			break;
		case GPS_SHOW_LARGE:
			if ((m_nMode == 0)||(m_nMode == 3))
				dwTextType = 0x0F;
			else if (m_nMode == 1)
				dwTextType = (1<<CURRENT_TIME)|
							 (1<<START_END_TIME)|
							 (1<<EXT_END_TIME)|
							 (1<<LONGITUDE)|
							 (1<<LATITUDE);
			UpdateGPS_Large(dwTextType);

			break;
		}
	}
	TextOutGps(dwTextType);

	m_btnHide.Invalidate();
	m_btnLarge.Invalidate();
	m_btnNormal.Invalidate();
}


int CWndGps::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	LPST_WND_POS lpWndPos;
	lpWndPos = theApp.WndPos.GetAt(wpGpsView);

	m_nMode = lpWndPos->nMode;

	m_dwColorBg = lpWndPos->dwColorBackground;
	m_dwColorText = lpWndPos->dwColorText;
	m_dwColorTextBg = lpWndPos->dwColorLevel[0];
	m_dwColorMarker = lpWndPos->dwColorLevel[1];
	m_dwColorTextTitle = lpWndPos->dwColorLevel[2];


	m_penEdge.CreatePen(PS_SOLID, 2, m_dwColorBg);		//2634310
	m_brushBoard.CreateSolidBrush(m_dwColorBg);
	m_brushTextBk.CreateSolidBrush(m_dwColorTextBg);		//4015456
	m_brushMark.CreateSolidBrush(m_dwColorMarker);			//3307770

	int i;
	LPST_BMP_INFO lpBmpInfo;
	for (i=0;i<4;i++)
	{
		lpBmpInfo = theApp.BmpInfo.GetAt(biGpsNormal_normal+i);
		m_btnNormal.SetBitmaps(i, lpBmpInfo->hbmpImage);
		lpBmpInfo = theApp.BmpInfo.GetAt(biGpsHide_normal+i);
		m_btnHide.SetBitmaps(i, lpBmpInfo->hbmpImage);
		lpBmpInfo = theApp.BmpInfo.GetAt(biGpsLarge_normal+i);
		m_btnLarge.SetBitmaps(i, lpBmpInfo->hbmpImage);
	}

//	LPST_BMP_INFO lpBmpInfo;
	lpBmpInfo = theApp.BmpInfo.GetAt(biGpsLarge_normal);
	if (!m_btnLarge.Create(NULL, NULL, WS_CHILD|WS_VISIBLE, CRect(0, 0, 0, 0), this, lpBmpInfo->uImageID, NULL))
		return -1;

	lpBmpInfo = theApp.BmpInfo.GetAt(biGpsNormal_normal);
	if (!m_btnNormal.Create(NULL, NULL, WS_CHILD|WS_VISIBLE, CRect(0, 0, 0, 0), this, lpBmpInfo->uImageID, NULL))
		return -1;

	lpBmpInfo = theApp.BmpInfo.GetAt(biGpsHide_normal);
	if (!m_btnHide.Create(NULL, NULL, WS_CHILD|WS_VISIBLE, CRect(0, 0, 0, 0), this, lpBmpInfo->uImageID, NULL))
		return -1;

	LoadText();

	m_btnLarge.ShowWindow(SW_SHOW);
	m_btnNormal.ShowWindow(SW_SHOW);
	m_btnHide.ShowWindow(SW_SHOW);

	UpdateButtonState();

	SetTimer(TIMER_ID_CURRENT_TIME, 1000, NULL);

	return 0;
}

void CWndGps::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	if ((cx<=0)||(cy<=0))
		return;

	LPST_WND_POS lpWndPos;
	lpWndPos = theApp.WndPos.GetAt(wpGpsBtnLarge);
	theApp.SetWindowPosition(&m_btnLarge, cx, cy, lpWndPos->rtWndPos, lpWndPos->rtStickTo);
	lpWndPos = theApp.WndPos.GetAt(wpGpsBtnNormal);
	theApp.SetWindowPosition(&m_btnNormal, cx, cy, lpWndPos->rtWndPos, lpWndPos->rtStickTo);
	lpWndPos = theApp.WndPos.GetAt(wpGpsBtnHide);
	theApp.SetWindowPosition(&m_btnHide, cx, cy, lpWndPos->rtWndPos, lpWndPos->rtStickTo);
}

BOOL CWndGps::PreTranslateMessage(MSG* pMsg)
{
	return CWnd::PreTranslateMessage(pMsg);
}

long CWndGps::OnCtlMessage(UINT wParam, LONG lParam)
{
	CWnd* pParent = GetParent();

	int nID = LOWORD(wParam);
	BOOL bClicked = FALSE;

	LPST_BMP_INFO lpBmpInfo;

	lpBmpInfo = theApp.BmpInfo.GetAt(biGpsHide_normal);
	if (nID == lpBmpInfo->uImageID)
	{
		m_nGpsWndMode = GPS_HIDE;
		bClicked = TRUE;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biGpsNormal_normal);
	if (nID == lpBmpInfo->uImageID)
	{
		m_nGpsWndMode = GPS_SHOW_NORMAL;
		bClicked = TRUE;
	}
	lpBmpInfo = theApp.BmpInfo.GetAt(biGpsLarge_normal);
	if (nID == lpBmpInfo->uImageID)
	{
		m_nGpsWndMode = GPS_SHOW_LARGE;
		bClicked = TRUE;
	}

	if (bClicked)
	{
		UpdateButtonState();
	}


	pParent->PostMessage(WM_CTL_MESSAGE, wParam, lParam);
	return 0;
}

void CWndGps::UpdateButtonState()
{
	switch (m_nGpsWndMode)
	{
	case GPS_HIDE:
		m_btnHide.EnableWindow(FALSE);
		m_btnLarge.EnableWindow(TRUE);
		m_btnNormal.EnableWindow(TRUE);
		break;
	case GPS_SHOW_LARGE:
		m_btnHide.EnableWindow(TRUE);
		m_btnLarge.EnableWindow(FALSE);
		m_btnNormal.EnableWindow(TRUE);
		break;
	case GPS_SHOW_NORMAL:
		m_btnHide.EnableWindow(TRUE);
		m_btnLarge.EnableWindow(TRUE);
		m_btnNormal.EnableWindow(FALSE);
		break;
	}
}

void CWndGps::OnTimer(UINT_PTR nIDEvent)
{
	if (nIDEvent == TIMER_ID_CURRENT_TIME)
	{
		if (!m_bPlayOn)
		{
			m_tGpsTime = time(0) - m_TimeZone.GetTimeZone();
			TextOutGps((1<<CURRENT_TIME));
		}
	}

	CWnd::OnTimer(nIDEvent);
}

void CWndGps::OnDestroy()
{
	CWnd::OnDestroy();

	KillTimer(TIMER_ID_CURRENT_TIME);
}

void CWndGps::SetButtonState(BOOL bEnable)
{
	if (bEnable)
		UpdateButtonState();
	else
	{
		m_btnHide.EnableWindow(bEnable);
		m_btnLarge.EnableWindow(bEnable);
		m_btnNormal.EnableWindow(bEnable);
	}
}

//////////////////////////////////////////////////////////////////////////
//mode 0
void CWndGps::TextOutGps_m0(DWORD dwTextType)
{
	CDC *pDC = GetDC();		//(1) - get
	CDC dcMemory;
	CBitmap bmp, *pOldBmp;
	CFont *pOldFont;
	CPen *pOldPen;
	CBrush *pOldBrush;
	BOOL bTextOut[TEXT_COUNT];
	TCHAR tzTextOut[TEXT_COUNT][128];
	CUtilString us;

	int i;

	ZeroMemory(bTextOut, sizeof(bTextOut));		//set all to FALSE;
	if (dwTextType&(1<<START_END_TIME))
	{
		bTextOut[START_END_TIME] = TRUE;
		if (m_bPlayOn)
		{
			TCHAR tzStartTime[64], tzEndTime[64];
			wsprintf(tzStartTime, _T("%s"), us.GetDateTimeString(m_tStartTime));
			wsprintf(tzEndTime, _T("%s"), us.GetDateTimeString(m_tEndTime));
			wsprintf(tzTextOut[START_END_TIME], _T("%s  /  %s"), tzStartTime, tzEndTime);

		}
		else
		{
			wsprintf(tzTextOut[START_END_TIME], _T("%s"), m_sarrText.GetAt(Gps_Status));
		}
	}
	if (dwTextType&(1<<CURRENT_TIME))
	{
		bTextOut[CURRENT_TIME] = TRUE;
		wsprintf(tzTextOut[CURRENT_TIME], _T("%s"), us.GetDateTimeString(m_tGpsTime));
	}
	if (dwTextType&(1<<LONGITUDE))
	{
		bTextOut[LONGITUDE] = TRUE;
		wsprintf(tzTextOut[LONGITUDE], _T("%s"), us.GetGpsLongitude(m_fLongitude));

		TRACE(_T("long %f, %s"), m_fLongitude, us.GetGpsLongitude(m_fLongitude)); 
	}
	if (dwTextType&(1<<LATITUDE))
	{
		bTextOut[LATITUDE] = TRUE;
		wsprintf(tzTextOut[LATITUDE], _T("%s"), us.GetGpsLatitude(m_fLatitude));

		//TRACE(_T("long %f, %s"), m_fLatitude, us.GetGpsLatitude(m_fLatitude)); 
	}

	for (i=0;i<TEXT_COUNT;i++)
	{
		if (bTextOut[i])
		{
			bmp.CreateCompatibleBitmap(pDC, m_rtText[i].Width(), m_rtText[i].Height());		//(2) - create
			dcMemory.CreateCompatibleDC(pDC);												//(3) - create
			pOldBmp = dcMemory.SelectObject(&bmp);											//(4) - select

			pOldFont = dcMemory.SelectObject(&m_fntText);
			pOldBrush = dcMemory.SelectObject(&m_brushTextBk);
			pOldPen = dcMemory.SelectObject(&m_penEdge);

			dcMemory.Rectangle(0, 0, m_rtText[i].Width(), m_rtText[i].Height());

			dcMemory.SetBkMode(TRANSPARENT);
			dcMemory.SetTextColor(m_dwColorText);
			dcMemory.TextOut(4, 3, tzTextOut[i]);

			pDC->BitBlt(m_rtText[i].left, m_rtText[i].top, m_rtText[i].Width(),  m_rtText[i].Height(), &dcMemory, 0, 0, SRCCOPY);

			dcMemory.SelectObject(pOldPen);
			dcMemory.SelectObject(pOldBrush);
			dcMemory.SelectObject(pOldFont);
			dcMemory.SelectObject(pOldBmp);				//(4) de-select
			bmp.DeleteObject();							//(2) delete
			dcMemory.DeleteDC();						//(3) delete
		}
	}
	ReleaseDC(pDC);		//(1) - release

}

void CWndGps::UpdateGPS_Normal_m0()
{
	CDC *pDC = GetDC();
	CDC dcMemory;
	CBitmap bmp, *pOldBmp;
	CRect rect;
	CPen *pOldPen;
	CBrush *pOldBursh;
	CFont *pOldFont;

	this->GetClientRect(&rect);
	bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	dcMemory.CreateCompatibleDC(pDC);
	pOldBmp = dcMemory.SelectObject(&bmp);

	pOldPen = dcMemory.SelectObject(&m_penEdge);

	pOldBursh = dcMemory.SelectObject(&m_brushBoard);
	dcMemory.Rectangle(&rect);
	dcMemory.SelectObject(pOldBursh);

	CRect rtText;
	CRect rtMark;

	rtText.left = TEXT_BOX_LEFT;
	rtText.right = rect.right-TEXT_BOX_RIGHT;
	rtText.top = rect.top;
	rtText.bottom = rect.bottom;

	m_rtText[CURRENT_TIME] = rtText;

	rtMark.left = 4;
	rtMark.right = rtMark.left+8;
	rtMark.top = rect.Height()/2-4;
	rtMark.bottom = rtMark.top + 8;

	pOldBursh = dcMemory.SelectObject(&m_brushTextBk);
	dcMemory.Rectangle(&rtText);
	dcMemory.SelectObject(pOldBursh);

	pOldBursh = dcMemory.SelectObject(&m_brushMark);
	dcMemory.Rectangle(&rtMark);
	dcMemory.SelectObject(pOldBursh);

	dcMemory.SelectObject(pOldPen);

	pOldFont = dcMemory.SelectObject(&m_fntText);

	dcMemory.SetBkMode(TRANSPARENT);
	dcMemory.SetTextColor(m_dwColorTextTitle);

	dcMemory.TextOut(rtMark.right+4, rtText.top+3, m_sarrText.GetAt(Gps_Time));

	dcMemory.SelectObject(pOldFont);

	pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dcMemory, 0, 0, SRCCOPY);

	dcMemory.SelectObject(pOldBmp);
	dcMemory.DeleteDC();
	bmp.DeleteObject();			//2013.04.29
	ReleaseDC(pDC);

}

void CWndGps::UpdateGPS_Normal_m3()
{
	CDC *pDC = GetDC();
	CDC dcMemory;
	CBitmap bmp, *pOldBmp;
	CRect rect;
	CPen *pOldPen;
	CBrush *pOldBursh;
	CFont *pOldFont;

	this->GetClientRect(&rect);
	bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	dcMemory.CreateCompatibleDC(pDC);
	pOldBmp = dcMemory.SelectObject(&bmp);

	pOldPen = dcMemory.SelectObject(&m_penEdge);

	pOldBursh = dcMemory.SelectObject(&m_brushBoard);
	dcMemory.Rectangle(&rect);
	dcMemory.SelectObject(pOldBursh);

	CRect rtText;
	CRect rtMark;

	rtText.left = TEXT_BOX_LEFT;
	rtText.right = rect.right-TEXT_BOX_RIGHT;
	rtText.top = rect.top;
	rtText.bottom = rtText.top + rect.Height()/2;

	m_rtText[CURRENT_TIME] = rtText;

	rtMark.left = 4;
	rtMark.right = rtMark.left+8;
	rtMark.top = rect.Height()/4-4;
	rtMark.bottom = rtMark.top + 8;

	pOldBursh = dcMemory.SelectObject(&m_brushTextBk);
	dcMemory.Rectangle(&rtText);
	dcMemory.SelectObject(pOldBursh);

	pOldBursh = dcMemory.SelectObject(&m_brushMark);
	dcMemory.Rectangle(&rtMark);
	dcMemory.SelectObject(pOldBursh);

	rtText.top += rect.Height()/2;
	rtText.bottom += rect.Height()/2;
	rtText.left = 2;
	rtText.right = rtText.left + rect.Width()-4;
	m_rtText[START_END_TIME] = rtText;


	pOldBursh = dcMemory.SelectObject(&m_brushTextBk);
	dcMemory.Rectangle(&rtText);
	dcMemory.SelectObject(pOldBursh);

// 	pOldBursh = dcMemory.SelectObject(&m_brushMark);
// 	dcMemory.Rectangle(&rtMark);
// 	dcMemory.SelectObject(pOldBursh);

	dcMemory.SelectObject(pOldPen);

	pOldFont = dcMemory.SelectObject(&m_fntText);

	dcMemory.SetBkMode(TRANSPARENT);
	dcMemory.SetTextColor(m_dwColorTextTitle);

	dcMemory.TextOut(rtMark.right+4, m_rtText[CURRENT_TIME].top+3, m_sarrText.GetAt(Gps_Time));

	dcMemory.SelectObject(pOldFont);

	pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dcMemory, 0, 0, SRCCOPY);

	dcMemory.SelectObject(pOldBmp);
	dcMemory.DeleteDC();
	bmp.DeleteObject();			//2013.04.29
	ReleaseDC(pDC);

}

void CWndGps::UpdateGPS_Large_m0()
{
	CDC *pDC = GetDC();
	CDC dcMemory;
	CBitmap bmp, *pOldBmp;
	CRect rect;
	CPen *pOldPen;
	CBrush *pOldBursh;
	CFont *pOldFont;

	CRect rtText[TEXT_COUNT], rtMark[TEXT_COUNT];
	CRect rtCompass;
	CString strText[TEXT_COUNT];

	strText[START_END_TIME] = m_sarrText.GetAt(Gps_Title);
	strText[CURRENT_TIME] = m_sarrText.GetAt(Gps_Time);
	strText[LATITUDE] = m_sarrText.GetAt(Gps_Lat);
	strText[LONGITUDE] = m_sarrText.GetAt(Gps_Long);

	this->GetClientRect(&rect);

	int nInterval, i, nTopGap;
	nInterval = (rect.Height()-10)/5;
	nTopGap = rect.Height() - nInterval*5;
	rtText[START_END_TIME].left = rect.left;
	rtText[START_END_TIME].right = rect.right;
	rtText[START_END_TIME].top = nTopGap+nInterval;
	rtText[START_END_TIME].bottom = rtText[START_END_TIME].top + nInterval;

	rtCompass.top = nTopGap+nInterval*2;
	rtCompass.bottom = rect.bottom;
	rtCompass.right = rect.right;
	rtCompass.left = rect.right - rtCompass.Height();

	for (i=1;i<TEXT_COUNT;i++)
	{
		rtText[i].left = (rect.Width() - rtCompass.Width())/3;
		rtText[i].right = rtCompass.left;
		rtText[i].top = nTopGap+nInterval*(i+1);
		rtText[i].bottom = rtText[i].top + nInterval;

		rtMark[i].top = rtText[i].top + nInterval/2 - 4;
		rtMark[i].bottom = rtMark[i].top + 8;
		rtMark[i].left = 4;
		rtMark[i].right = 12;
	}

	for (i=0;i<TEXT_COUNT;i++)
		m_rtText[i] = rtText[i];

	bmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	dcMemory.CreateCompatibleDC(pDC);
	pOldBmp = dcMemory.SelectObject(&bmp);

	pOldPen = dcMemory.SelectObject(&m_penEdge);

	pOldBursh = dcMemory.SelectObject(&m_brushBoard);
	dcMemory.Rectangle(&rect);
	dcMemory.SelectObject(pOldBursh);

	pOldBursh = dcMemory.SelectObject(&m_brushTextBk);
	for (i=0;i<TEXT_COUNT;i++)
	{
		dcMemory.Rectangle(&rtText[i]);
	}
	dcMemory.Rectangle(&rtCompass);
	dcMemory.SelectObject(pOldBursh);

	pOldBursh = dcMemory.SelectObject(&m_brushMark);
	for (i=1;i<TEXT_COUNT;i++)
		dcMemory.Rectangle(&rtMark[i]);
	dcMemory.SelectObject(pOldBursh);


	dcMemory.SelectObject(pOldPen);

	pOldFont = dcMemory.SelectObject(&m_fntText);

	dcMemory.SetBkMode(TRANSPARENT);
	dcMemory.SetTextColor(m_dwColorTextTitle);

	dcMemory.TextOut(8, nTopGap+3, strText[START_END_TIME]);
	for (i=1;i<TEXT_COUNT;i++)
		dcMemory.TextOut(rtMark[i].right+4, rtText[i].top + 3, strText[i]);

	dcMemory.SelectObject(pOldFont);
	pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dcMemory, 0, 0, SRCCOPY);

	dcMemory.SelectObject(pOldBmp);
	bmp.DeleteObject();
	dcMemory.DeleteDC();
	ReleaseDC(pDC);

}

//////////////////////////////////////////////////////////////////////////
//mode 1
void CWndGps::TextOutGps_m1(DWORD dwTextType)
{
	CDC *pDC = GetDC();
	CDC dcMemory;
	CBitmap bmp, *pOldBmp;
	CFont *pOldFont;
	CPen *pOldPen;
	CBrush *pOldBrush;
	BOOL bTextOut[TEXT_COUNT];
	TCHAR tzTextOut[TEXT_COUNT][128];
	CUtilString us;

	int i;

	ZeroMemory(bTextOut, sizeof(bTextOut));		//set all to FALSE;
	if (dwTextType&(1<<START_END_TIME))
	{
		bTextOut[START_END_TIME] = TRUE;
		if (m_bPlayOn)
			wsprintf(tzTextOut[START_END_TIME], _T("%s"), us.GetDateTimeString(m_tStartTime));
		else
			wsprintf(tzTextOut[START_END_TIME], _T("%s"), m_sarrText.GetAt(Gps_Status));
	}
	if (dwTextType&(1<<EXT_END_TIME))
	{
		bTextOut[EXT_END_TIME] = TRUE;
		if (m_bPlayOn)
			wsprintf(tzTextOut[EXT_END_TIME], _T("%s"), us.GetDateTimeString(m_tEndTime));
		else
			wsprintf(tzTextOut[EXT_END_TIME], _T("%s"), m_sarrText.GetAt(Gps_Status));
	}
	if (dwTextType&(1<<CURRENT_TIME))
	{
		bTextOut[CURRENT_TIME] = TRUE;
		wsprintf(tzTextOut[CURRENT_TIME], _T("%s"), us.GetDateTimeString(m_tGpsTime));
	}
	if (dwTextType&(1<<LONGITUDE))
	{
		bTextOut[LONGITUDE] = TRUE;
		wsprintf(tzTextOut[LONGITUDE], _T("%s"), us.GetGpsLongitude(m_fLongitude));
	}
	if (dwTextType&(1<<LATITUDE))
	{
		bTextOut[LATITUDE] = TRUE;
		wsprintf(tzTextOut[LATITUDE], _T("%s"), us.GetGpsLatitude(m_fLatitude));
	}
	if (dwTextType&(1<<DRIVING_SPEED))
	{
		bTextOut[DRIVING_SPEED] = TRUE;

		if (m_nUnit == 0)
		{
			wsprintf(tzTextOut[DRIVING_SPEED], _T("%d Km/h"), (int)(FLOAT)(m_fSpeed*1.852));
		}
		else
		{
			wsprintf(tzTextOut[DRIVING_SPEED], _T("%d MPH"), (int)(FLOAT)(m_fSpeed*1.150));
		}
	}

	LOGFONT logFont;
	int nHeight;
	m_fntText.GetLogFont(&logFont);
	nHeight = logFont.lfHeight;

	static int nOrder[] = {
		3, 5, 0, 1, 4, 2
	};

	for (i=0;i<TEXT_COUNT;i++)
	{
		if (bTextOut[i])
		{
			LPST_WND_POS lpWndPos;
			lpWndPos = theApp.WndPos.GetAt(wpGpsView);
			bmp.CreateCompatibleBitmap(pDC, m_rtText[nOrder[i]].Width(), m_rtText[nOrder[i]].Height());
			dcMemory.CreateCompatibleDC(pDC);
			pOldBmp = dcMemory.SelectObject(&bmp);

			pOldFont = dcMemory.SelectObject(&m_fntText);
			pOldBrush = dcMemory.SelectObject(&m_brushTextBk);
			pOldPen = dcMemory.SelectObject(&m_penEdge);

			dcMemory.Rectangle(0, 0, m_rtText[nOrder[i]].Width(), m_rtText[nOrder[i]].Height());

			dcMemory.SetBkMode(TRANSPARENT);
			dcMemory.SetTextColor(m_dwColorText);
			dcMemory.TextOut(8, (m_rtText[nOrder[i]].Height()-nHeight)/2, tzTextOut[i]);

			pDC->BitBlt(m_rtText[nOrder[i]].left + lpWndPos->rtStickTo.left, 
						m_rtText[nOrder[i]].top + lpWndPos->rtStickTo.top, 
						m_rtText[nOrder[i]].Width(),  m_rtText[nOrder[i]].Height(), &dcMemory, 0, 0, SRCCOPY);

			dcMemory.SelectObject(pOldPen);
			dcMemory.SelectObject(pOldBrush);
			dcMemory.SelectObject(pOldFont);
			dcMemory.SelectObject(pOldBmp);
			bmp.DeleteObject();
			dcMemory.DeleteDC();
		}
	}
	ReleaseDC(pDC);
}

void CWndGps::UpdateGPS_Normal_m1()
{
	CDC *pDC = GetDC();
	CDC dcMemory;
	CBitmap bmp, *pOldBmp;
	CRect rect, rectDC;
	CPen *pOldPen;
	CBrush *pOldBursh;
	CFont *pOldFont;

	this->GetClientRect(&rect);

	LPST_WND_POS lpWndPos;
	lpWndPos = theApp.WndPos.GetAt(wpGpsView);
	rect.DeflateRect(lpWndPos->rtStickTo.left,
					 lpWndPos->rtStickTo.top,
					 lpWndPos->rtStickTo.right,
					 lpWndPos->rtStickTo.bottom);
	rectDC.left = 0;
	rectDC.right = rect.Width();
	rectDC.top = 0;
	rectDC.bottom = rect.Height();

	bmp.CreateCompatibleBitmap(pDC, rectDC.Width(), rectDC.Height());
	dcMemory.CreateCompatibleDC(pDC);
	pOldBmp = dcMemory.SelectObject(&bmp);

	pOldPen = dcMemory.SelectObject(&m_penEdge);

	pOldBursh = dcMemory.SelectObject(&m_brushBoard);
	dcMemory.Rectangle(&rectDC);
	dcMemory.SelectObject(pOldBursh);

	CRect rtText;
	CRect rtMark;

	lpWndPos = theApp.WndPos.GetAt(wpGpsTextBox);
	rtText.left = lpWndPos->rtStickTo.left;
	rtText.right = rectDC.right-lpWndPos->rtStickTo.right;
	rtText.top = rectDC.top;
	rtText.bottom = rectDC.bottom;

	m_rtText[5] = rtText;

	rtMark.left = 4;
	rtMark.right = rtMark.left+8;
	rtMark.top = rectDC.Height()/2-4;
	rtMark.bottom = rtMark.top + 8;

	pOldBursh = dcMemory.SelectObject(&m_brushTextBk);
	dcMemory.Rectangle(&rtText);
	dcMemory.SelectObject(pOldBursh);

	pOldBursh = dcMemory.SelectObject(&m_brushMark);
	dcMemory.Rectangle(&rtMark);
	dcMemory.SelectObject(pOldBursh);

	dcMemory.SelectObject(pOldPen);

	pOldFont = dcMemory.SelectObject(&m_fntText);

	dcMemory.SetBkMode(TRANSPARENT);
	dcMemory.SetTextColor(m_dwColorTextTitle);

	int nHeight;
	LOGFONT logFont;
	m_fntText.GetLogFont(&logFont);
	nHeight = logFont.lfHeight;
	dcMemory.TextOut(rtMark.right+4, rtText.top+(rtText.Height()-nHeight)/2, m_sarrText.GetAt(Gps_Time));

	dcMemory.SelectObject(pOldFont);

	pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &dcMemory, 0, 0, SRCCOPY);

	dcMemory.SelectObject(pOldBmp);
	bmp.DeleteObject();			//2013.04.29
	dcMemory.DeleteDC();
	ReleaseDC(pDC);
}

void CWndGps::UpdateGPS_Large_m1(DWORD dwTextType)
{
	CDC *pDC = GetDC();
	CDC dcMemory;
	CBitmap bmp, *pOldBmp;
	CRect rect, rectDC;
	CPen *pOldPen;
	CBrush *pOldBursh;
	CFont *pOldFont;

	CRect rtText[TEXT_COUNT], rtMark[TEXT_COUNT];
	CRect rtCompass;
	CString strText[TEXT_COUNT];
	LPST_WND_POS lpWndPos;
	int i, nTopGap;
	int nCount = 0;
	for (i=0;i<TEXT_COUNT;i++)
	{
		if ((dwTextType>>i)&1)
			nCount++;
	}

// 	static int nOrder[] = {
// 		LATITUDE, LONGITUDE, DRIVING_SPEED, START_END_TIME, EXT_END_TIME, CURRENT_TIME
// 	};
	static int nOrder[] = {
		3, 5, 0, 1, 4, 2
	};

	DWORD dwTextTypeTemp = 0;

	for (i=0;i<TEXT_COUNT;i++)
	{
		if ((dwTextType>>i)&1)
		{
			dwTextTypeTemp |= 1<<nOrder[i];
		}
	}
	dwTextType = dwTextTypeTemp;


	i=0;
	strText[i++] = m_sarrText.GetAt(Gps_Lat);
	strText[i++] = m_sarrText.GetAt(Gps_Long);
	strText[i++] = m_sarrText.GetAt(Gps_speed);
	strText[i++] = m_sarrText.GetAt(Gps_Time_Start);
	strText[i++] = m_sarrText.GetAt(Gps_Time_End);
	strText[i++] = m_sarrText.GetAt(Gps_Time);

	lpWndPos = theApp.WndPos.GetAt(wpGpsView);
	this->GetClientRect(&rect);
	rect.DeflateRect(lpWndPos->rtStickTo.left,
					 lpWndPos->rtStickTo.top,
					 lpWndPos->rtStickTo.right,
					 lpWndPos->rtStickTo.bottom);
	rectDC.left = 0;
	rectDC.right = rect.Width();
	rectDC.top = 0;
	rectDC.bottom = rect.Height();


	nTopGap = rectDC.Height()/nCount;
	i = 0;
	lpWndPos = theApp.WndPos.GetAt(wpGpsTextBox);


	for (i=0,nCount=0;i<TEXT_COUNT;i++)
	{
		if ((dwTextType>>i)&1)
		{
			rtText[i].left = lpWndPos->rtStickTo.left;
			rtText[i].right = rectDC.right-lpWndPos->rtStickTo.right;
			rtText[i].top = nTopGap*nCount;
			rtText[i].bottom = nTopGap*(nCount+1);
			nCount++;
		}
	}

	for (i=0, nCount=0;i<TEXT_COUNT;i++)
	{
		if ((dwTextType>>i)&1)
		{
			rtMark[i].left = 4;
			rtMark[i].right = rtMark[i].left+8;
			rtMark[i].top = nTopGap*nCount+nTopGap/2-4;
			rtMark[i].bottom = rtMark[i].top + 8;
			nCount++;
		}
	}

	rtCompass.top = 0;
	rtCompass.bottom = nTopGap*4;
	rtCompass.right = rect.right;
	rtCompass.left = rect.right - lpWndPos->rtStickTo.right;

	for (i=0;i<TEXT_COUNT;i++)
		m_rtText[i] = rtText[i];

	bmp.CreateCompatibleBitmap(pDC, rectDC.Width(), rectDC.Height());
	dcMemory.CreateCompatibleDC(pDC);
	pOldBmp = dcMemory.SelectObject(&bmp);

	pOldPen = dcMemory.SelectObject(&m_penEdge);

	pOldBursh = dcMemory.SelectObject(&m_brushBoard);
	dcMemory.Rectangle(&rectDC);
	dcMemory.SelectObject(pOldBursh);

	pOldBursh = dcMemory.SelectObject(&m_brushTextBk);
	for (i=0;i<TEXT_COUNT;i++)
	{
		if ((dwTextType>>i)&1)
		{
			dcMemory.Rectangle(&rtText[i]);
		}
	}

	dcMemory.SelectObject(pOldBursh);

	pOldBursh = dcMemory.SelectObject(&m_brushMark);
	for (i=0;i<TEXT_COUNT;i++)
	{
		if ((dwTextType>>i)&1)
		{
			dcMemory.Rectangle(&rtMark[i]);
		}
	}
	dcMemory.SelectObject(pOldBursh);


	dcMemory.SelectObject(pOldPen);

	pOldFont = dcMemory.SelectObject(&m_fntText);

	dcMemory.SetBkMode(TRANSPARENT);
	dcMemory.SetTextColor(m_dwColorTextTitle);

	LOGFONT logFont;
	int nHeight;
	m_fntText.GetLogFont(&logFont);
	nHeight = logFont.lfHeight;
	for (i=0,nCount=0;i<TEXT_COUNT;i++)
	{
		if ((dwTextType>>i)&1)
		{
			dcMemory.TextOut(rtMark[i].right+4, rtText[i].top + (rtText[i].Height()-nHeight)/2, strText[i]);
			nCount++;
		}
	}

	dcMemory.SelectObject(pOldFont);
	pDC->BitBlt(rect.left, rect.top, rectDC.Width(), rectDC.Height(), &dcMemory, 0, 0, SRCCOPY);

	dcMemory.SelectObject(pOldBmp);
	bmp.DeleteObject();		//2013.04.29
	dcMemory.DeleteDC();
	ReleaseDC(pDC);
}

void CWndGps::RefreshLang()
{
	LoadText();

	Invalidate(TRUE);
}
//////////////////////////////////////////////////////////////////////////
void CWndGps::LoadText()
{
	m_sarrText.RemoveAll();

	m_sarrText.Add(theApp.GetUIString(APPNAME_GPS, KEYNAME_GPS_TITLE, _T("Start Time / End Time")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GPS, KEYNAME_GPS_STATUS, _T("Playing is stopped")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GPS, KEYNAME_GPS_TIME, _T("Time")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GPS, KEYNAME_GPS_LATITUDE, _T("Latitude")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GPS, KEYNAME_GPS_LONGITUDE, _T("Longitude")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GPS, KEYNAME_GPS_TIME_START, _T("Start Time")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GPS, KEYNAME_GPS_TIME_END, _T("End Time")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_GPS, KEYNAME_GPS_SPEED, _T("Speed")));

}